﻿using System.Windows;
using EzDesk.DesktopClient.AppModule.View;
using EzDesk.DesktopClient.AppModule.ViewContract;
using EzDesk.DesktopClient.AppModule.ViewModel;
using EzDesk.Modularity;
using EzDesk.DesktopClient.Event;
using EzDesk.Generic.WPF;
using EzDesk.EventAggregation;
using Microsoft.Practices.Unity;

namespace EzDesk.DesktopClient.AppModule
{
    public class AppModuleInfo : IModule
    {
        public Window MainPanelWindow { get; private set; }
        public IModule Initialize()
        {
            RegistView();

            LoginEvent.Instance.Subscribe(this.OnUserLogin, ThreadOption.UIThread);
            LogoutEvent.Instance.Subscribe(this.OnUserLogout);
            return this;
        }

        private static void RegistView()
        {
            var container =IMClientUnityContainer.Instance;
            container.RegisterType<IMainPanelView, MainPanelView>();
            container.RegisterType<IMoreFunctionPanelView, MoreFunctionPanelView>();
            container.RegisterType<IShortcutFunctionPanelView, ShortcutFunctionPanelView>();   
        }

        protected void OnUserLogin(LoginArgs args)
        {
            MainPanelWindow = new MainPanelViewModel().View as Window;
            MainPanelWindow.Show();
            MainPanelWindow.ToBesideScreen();
        }

        protected void OnUserLogout(LogoutArgs args)
        {
            if (MainPanelWindow != null)
            {
                MainPanelWindow.Close();
                MainPanelWindow = null;
            }       
        }
    }
}
